//
//  CategoryPopView.m
//  LvXi_newVersion
//
//  Created by 王敏 on 16/8/30.
//  Copyright © 2016年 SJT. All rights reserved.
//

#import "CategoryPopView.h"

#import "SegmentView.h"

#import "PopCollectionCell.h"

@interface CategoryPopView()<UITableViewDelegate,UITableViewDataSource,UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout,SegmentViewDelegate>{
    BOOL _isHidden;
}
@property(nonatomic,strong)SegmentView * segmentView;

@property(nonatomic,strong)UITableView * tableView;

@property(nonatomic,strong)UICollectionView *collectionView;

@property(nonatomic,strong)NSArray * secondCategoryArray;

@property(nonatomic,strong)NSArray * thirdCategoryArray;
@end

@implementation CategoryPopView

NSString * imageItemID = @"PopCollectionCell";

-(id)initWithFrame:(CGRect)frame{
    if(self = [super initWithFrame:frame]){
        self.backgroundColor = [UIColor whiteColor];
        [self addSubview:self.segmentView];
        self.segmentView.options = @[@"营养保健",@"零食坚果",@"母婴用品",@"海鲜干货"];
        [self addSubview:self.tableView];
        [self addSubview:self.collectionView];
    }
    return self;
}



-(SegmentView*)segmentView{
    if(_segmentView == nil){
        _segmentView = [[SegmentView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, Category_height(44))];
        _segmentView.delegate = self;
        _segmentView.backgroundColor = [UIColor whiteColor];
    }
    return _segmentView;
}

-(void)setCategoryArray:(NSArray *)categoryArray{
    _categoryArray = categoryArray;
    NSMutableArray * options = [NSMutableArray array];
    for (CategoryModel*model in _categoryArray) {
        [options addObject:model.cat_name];
    }
    self.segmentView.options = options;
    self.secondCategoryArray = [[_categoryArray firstObject] sublist];
    self.thirdCategoryArray = [[self.secondCategoryArray firstObject] sublist];
}

#pragma mark - 一级分类
-(void)didSelectedOptionIndex:(NSInteger)index segmentView:(SegmentView *)segmentView{
    self.secondCategoryArray = [self.categoryArray[index] sublist];
    self.thirdCategoryArray = [[self.secondCategoryArray firstObject] sublist];
    [self.tableView reloadData];
    [self.collectionView reloadData];
}

#pragma mark - tableView delegate dataSource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.secondCategoryArray?self.secondCategoryArray.count:0;
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell * cell = [[UITableViewCell alloc] init];
    if(self.secondCategoryArray){
        cell.textLabel.text = [self.secondCategoryArray[indexPath.row] cat_name];

    }
    cell.textLabel.font = [UIFont systemFontOfSize:15];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    self.thirdCategoryArray = [self.secondCategoryArray[indexPath.row] sublist];
    [self.collectionView reloadData];
}



#pragma mark - collectionView delegate dataSource UICollectionViewFlowLayout


-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.thirdCategoryArray?self.thirdCategoryArray.count:0;;
}


-(UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    PopCollectionCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:imageItemID forIndexPath:indexPath];
    if(self.thirdCategoryArray){
        cell.category = self.thirdCategoryArray[indexPath.item];
    }
    return cell;
}


-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    CategoryModel * cat = self.thirdCategoryArray[indexPath.item];
    self.cat_id = cat.cat_id;
    if([self.delegate respondsToSelector:@selector(CategoryPopView:didSelectedWithCat_id:)]){
        [self.delegate CategoryPopView:self didSelectedWithCat_id:self.cat_id];
    }
}

-(UICollectionView*)collectionView{
    if(_collectionView == nil){
        
        CGFloat collectionViewWidth = self.width - self.tableView.width;
        CGFloat collectionViewheight = self.tableView.height;
        //创建一个layout布局类
        UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc]init];
        //设置布局方向为垂直流布局
        layout.scrollDirection = UICollectionViewScrollDirectionVertical;
        //设置每个item
        CGFloat itemWidth = (collectionViewWidth-4*globalMargin)/3.0;
        CGFloat itemHeight = itemWidth*1.4;
        layout.itemSize = CGSizeMake(itemWidth, itemHeight);
        layout.minimumLineSpacing = 0;
        layout.minimumInteritemSpacing = globalMargin;
        layout.sectionInset = UIEdgeInsetsMake(globalMargin,globalMargin,0,globalMargin);
        
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(self.tableView.right, self.tableView.y, collectionViewWidth, collectionViewheight) collectionViewLayout:layout];
        _collectionView.backgroundColor = HexColor(@"#EEEEEE");
        
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        
        [_collectionView registerNib:[UINib nibWithNibName:@"PopCollectionCell" bundle:nil] forCellWithReuseIdentifier:imageItemID];
        
    }
    return _collectionView;
}


-(UITableView*)tableView{
    if(_tableView == nil){
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, self.segmentView.botton, Category_width(94), self.height - self.segmentView.height) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.backgroundColor = HexColor(@"#EEEEEE");
    }
    return _tableView;
}
@end
